<template>

  <van-form @submit="onSubmit">
      <van-field
          v-model="editUser.currentValue"
          :name="editUser.editKey"
          :label="`请输入${editUser.editName}`"
      />
    <van-divider v-if="editUser.editKey === 'gender'">
      <van-highlight :keywords="keywords" :source-string="textOne" id="keywordsEditFontSize"/>
    </van-divider>
    <van-divider v-if="editUser.editKey === 'gender'">
    <van-highlight :keywords="keywords" :source-string="textTwo" id="keywordsEditFontSize"/>
    </van-divider>
    <van-divider v-if="editUser.editKey === 'gender'">
      <van-highlight :keywords="keywords" :source-string="textThree" id="keywordsEditFontSize"/>
    </van-divider>
    <div style="margin: 16px;" id="editUserTwoButton">
      <van-button round block type="primary" @click="cancelEditUser">
        取消
      </van-button>
      <van-button round block type="primary" native-type="submit">
        提交
      </van-button>
    </div>
  </van-form>

</template>


<script setup lang="ts">
import {useRoute, useRouter} from "vue-router";
import {ref} from "vue";
import myAxios from "../plugins/myAxios.ts";
import {showSuccessToast, showToast} from "vant";
import {showFailToast} from "vant";
import {getCurrentUser} from "../services/user.ts";

const route = useRoute();
const router = useRouter();

const editUser = ref({
  editKey: route.query.editKey,
  editName: route.query.editName,
  currentValue: route.query.currentValue,
})

const textOne = '请输入0或其它数字';
const textTwo = '0表示男';
const textThree = '其它数字表示女';
const keywords = ['0', '其它数字'];

const onSubmit = async () => {
  //获取用户信息
  const currentUser = await getCurrentUser();
  if (!currentUser) {
    showFailToast('用户未登录');
    return;
  }
  const res = await myAxios.post('/user/update', {
    'id': currentUser?.id,
    [editUser.value.editKey]: editUser.value.currentValue,
  })
  console.log(res, '更新请求');
  if(res.code === 0 && res.data > 0){
    showSuccessToast('修改成功');
    router.back();
  } else {
    showFailToast('修改失败');
  }
};

/**
 * 取消编辑用户信息
 */
const cancelEditUser = () => {
  showToast('取消编辑');
  router.back();
}
</script>


<style scoped>
#keywordsEditFontSize {
  font-size: 17px;
}
#editUserTwoButton {
  display: flex;
  justify-content: center;
}
</style>